<?php
/**
 * Created by PhpStorm.
 * Author: i@fedax.cn
 * Date: 2018/1/5
 * Time: 17:43
 */

namespace app\admin\controller;

use app\admin\model\AuthAccess;
use app\admin\model\Menu as MenuModel;
use think\Facade\Cache;
use think\Controller;
use app\admin\model\Admin as AdminModel;
/**
 * 首页
 * Class Index
 * @package app\admin\controller
 */
class Index extends Controller
{
    public $redis;

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function  initialize(){
//        $this->redis =  new \Redis();
//        $this->redis->connect(config('app.redis.host'), 6379);
//        $this->redis->auth(config('app.redis.pwd'));
        $adminModel = new AdminModel;
        $adminDataDetail = $adminModel->checkToken(session('admin_token'));
        if ($adminDataDetail['ret'] !== 200) {
            session('admin_token', null);
            $this->redirect('/Login');
        }
        //dump($this->checkRule());die;
        if (!$this->checkRule()) {
            $this->error("向超管申请权限");

        }
        $this->assign('admin_name', $adminDataDetail['data']['admin_name']);
    }

    /**
     * 框架首页
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index(){


//        if(Cache::store('redis')->get('menus')){
//            $menus = Cache::store('redis')->get('menus');
//            //return json(['code'=>333,'msg'=>34]);
//        }else{
            $adminMenuModel = new MenuModel();
            $menus = $adminMenuModel->getTree(0);
//            Cache::store('redis')->set('menus',$menus);
//        }
        $this->assign("menus", $menus);


        //return json(['code'=>200,'msg'=>$menus]);
        return view();
    }

    /**
     * 框架欢迎页面
     * @return \think\response\View
     */
    public function welcome(){
        return view();
    }

    /**
     * 排序
     * @param $model
     * @return bool
     */
    protected function listOrders($model)
    {
        if (!is_object($model)) {
            return false;
        }
        $pk  = $model->getPk(); //获取主键名称

        $ids = $this->request->post("list_orders/a");

        if (!empty($ids)) {
            foreach ($ids as $key => $r) {
                $data['list_order'] = $r;
                $model->where([$pk => $key])->update($data);
            }

        }
        return true;
    }

    /**
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function checkRule()
    {
        $rule_val = array();
        $rule_val['app'] = request()->module();// admin
        $rule_val['controller'] = request()->controller();// GivePresen
        $rule_val['action'] = request()->action();// givepresengiftname
        $rule_val['param'] = request()->query();// s=//GivePresenGiftName&prize_type=3
        //dump(request());die;
//        dump($rule_val);die;
//        $rule = strtolower($rule_val['app'] . '/' . $rule_val['controller'] . '/' . $rule_val['action']);
//        if (request()->query()) {
//            $rule = strtolower($rule_val['app'] . '/' . $rule_val['controller'] . '/' . $rule_val['action'] . '?' . $rule_val['param']);
//
//            //dump($rule);die; // admin/test/testindex?s=//testindex
//
//            $i1=strpos($rule,'?s=/');//开始位置
//            $i2=strpos($rule,'/&');//结束位置
//
//            if ($i1!==false && $i2!==false){//找到
//                //modify by 2020/3/20
//                //$rule=substr($rule,0,$i1) .'?'.substr($rule,$i2+strlen('/&'));
//                $rule=substr($rule,0,$i1) .'?'.substr($rule,$i2+strlen('&'));
//            }elseif ($i1!==false && $i2==false){
//                $rule=substr($rule,0,$i1);
//                //dump($rule);die;
//                //dump(233);die;
//            }
//        }
//        //add by 2020/3/20
//        if (is_string($rule)) {
//            if (strpos($rule, ',') !== false) {
//                $rule = explode(',', $rule);
//
//            } else {
//                $rule = [$rule];//dump($rule);die;
//            }
//        }
        //权限修改 官方赠送
//        if (request()->has('prize_type')) {
//            $extra_param =request()->only('prize_type');
//            $rule = http_build_query($extra_param);
//            $rule = strtolower($rule_val['app'] . '/' . $rule_val['controller'] . '/' . $rule_val['action']).'?'.$rule;
//        }else{
            $rule = strtolower($rule_val['app'] . '/' . $rule_val['controller'] . '/' . $rule_val['action']);
//        }

        //dump(session('admin_id'));die;
        $authModel = new AuthAccess();
        return $authModel->checkRule(session('admin_id'), $rule);
      // $res = $authModel->checkRule(session('admin_id'), $rule);

//       if(!$res) $this->error('没有权限');
//       else return $res;

    }
}